package com.zzj.ecology.framework.configs.security.handler;

import com.alibaba.fastjson.JSON;
import com.zzj.ecology.common.enums.LoginResultEnum;
import com.zzj.ecology.framework.utils.SecurityUtils;
import com.zzj.ecology.system.dto.HttpResult;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName CustomizeLogoutSuccessHandler
 * @Description 登出成功处理逻辑
 * @Author zhijun zeng at 2020/2/28
 * @Version 1.0
 **/
@Component
public class CustomizeLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication authentication) throws IOException, ServletException {

        String username = SecurityUtils.getUsername();

        resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);//
        resp.getWriter().write(JSON.toJSONString(new HttpResult(LoginResultEnum.USER_LOGOUT_SUCCESS)));
    }
}
